--[[-------------------------------------------------------------------------
  Copyright (c) 2006, Trond A Ekseth
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above
        copyright notice, this list of conditions and the following
        disclaimer in the documentation and/or other materials provided
        with the distribution.
      * Neither the name of Atropine nor the names of its contributors
        may be used to endorse or promote products derived from this
        software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------]]

local L = AtropineLocale
local G = getfenv(0)
local onKeyPressed = function(frame)
	if(frame:GetNumber() == 0) then frame:SetNumber(20) end
	this:ClearFocus()
end
local onClickLess = function(frame)
	frame = frame:GetParent().Count
	local t = frame:GetNumber()
	if(t > 1) then
		frame:SetNumber(t-1)
	end
end
local onClickMore = function(frame)
	frame = frame:GetParent().Count
	frame:SetText(frame:GetNumber()+1)
end
local onMouseWheel = function(frame, dir)
	if(dir == 1) then onClickMore(frame)
	else onClickLess(frame) end
end

local createFrame = function(fname, poison, texture, num)
	local b = G[fname] or CreateFrame("Frame", fname, AtropineFrame)

	AtropineFrame.Update:SetText(L["Update"])
	
	b:SetWidth(370)
	b:SetHeight(32)

	local icon = b.Icon or b:CreateTexture(nil, "ARTWORK")
	icon:SetTexture(texture)
	icon:SetWidth(24)
	icon:SetHeight(24)
	icon:SetPoint("LEFT", b, "LEFT", 0, 0)
	b.Icon = icon
	
	local background = b.Background or b:CreateTexture(nil, "ARTWORK")
	background:SetTexture("Interface\\QuestFrame\\UI-QuestItemNameFrame")
	background:SetWidth(200)
	background:SetHeight(30)
	background:SetPoint("LEFT", icon, "RIGHT", -12, 0)
	b.Background = background

	local text = b.Text or b:CreateFontString(nil, "OVERLAY")
	text:SetFont(STANDARD_TEXT_FONT, 12)
	text:SetWidth(165)
	text:SetHeight(15)
	text:SetPoint("CENTER", background, "CENTER",4, 0)
	text:SetTextColor(1, 1, 1)
	b.Text = text

	local less = b.Less or CreateFrame("Button", nil, b)
	less:SetWidth(16)
	less:SetHeight(16)
	less:SetPoint("LEFT", background, "RIGHT", -12, 0)
	less:SetScript("OnClick", onClickLess)
	less:SetNormalTexture("Interface\\MoneyFrame\\Arrow-Left-Up")
	less:SetPushedTexture("Interface\\MoneyFrame\\Arrow-Left-Down")
	less:SetDisabledTexture("Interface\\MoneyFrame\\Arrow-Left-Disabled")
	b.Less = less

	local countBG = b.CountBG or b:CreateTexture(nil, "ARTWORK")
	countBG:SetTexture("Interface\\QuestFrame\\UI-QuestItemNameFrame")
	countBG:SetWidth(40)
	countBG:SetHeight(30)
	countBG:SetPoint("LEFT", less, "RIGHT", 0, 0)
	b.CountBG = countBG

	local count = b.Count or CreateFrame("EditBox", nil, b)
	count:SetFont(STANDARD_TEXT_FONT, 12)
	count:SetAutoFocus(false)
	count:SetNumeric(true)
	count:EnableMouseWheel(true)
	count:SetScript("OnEnterPressed", onKeyPressed)
	count:SetScript("OnEscapePressed", onKeyPressed)
	count:SetScript("OnMouseWheel", onMouseWheel)
	count:SetJustifyH("CENTER")
	count:SetWidth(23)
	count:SetHeight(15)
	count:SetPoint("CENTER", countBG, "CENTER", 0, 0)
	b.Count = count

	local more = b.More or CreateFrame("Button",nil, b)
	more:SetWidth(16)
	more:SetHeight(16)
	more:SetPoint("LEFT", countBG, "RIGHT", 1, 0)
	more:SetScript("OnClick", onClickMore)
	more:SetNormalTexture("Interface\\MoneyFrame\\Arrow-Right-Up")
	more:SetPushedTexture("Interface\\MoneyFrame\\Arrow-Right-Down")
	more:SetDisabledTexture("Interface\\MoneyFrame\\Arrow-Right-Disabled")
	b.More = more

	local buy = b.Buy or CreateFrame("Button", nil, b, "UIPanelButtonGrayTemplate")
	buy:SetWidth(45)
	buy:SetHeight(24)
	buy:SetPoint("LEFT", more, "RIGHT", 1, 0)
	buy:SetFont(STANDARD_TEXT_FONT, 12)
	buy:SetText(L["Buy"])
	buy:SetScript("OnEnter", function()
		local c = count:GetNumber()
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT")
		GameTooltip:SetText(Atropine:getCost(poison, c))
	end)
	buy:SetScript("OnLeave", function()
		GameTooltip:Hide()
	end)
	buy:SetScript("OnClick", function()
		b:buy()
	end)
	b.Buy = buy

	local refill = b.Refill or CreateFrame("Button", nil, b, "UIPanelButtonGrayTemplate")
	refill:SetWidth(47)
	refill:SetHeight(24)
	refill:SetPoint("LEFT", buy, "RIGHT", 2, 0)
	refill:SetFont(STANDARD_TEXT_FONT, 12)
	refill:SetText(L["Refill"])
	refill:SetScript("OnEnter", function()
		local c = count:GetNumber()
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT")
		GameTooltip:SetText(Atropine:getCost(poison, c, true))
	end)
	refill:SetScript("OnLeave", function()
		GameTooltip:Hide()
	end)
	refill:SetScript("OnClick", function()
		b:refill()
	end)
	b.Refill = refill

	local check = b.Check or CreateFrame("CheckButton", nil, b)
	check:SetWidth(16)
	check:SetHeight(16)
	check:SetID(num)
	check:SetPoint("LEFT", refill, "RIGHT", 2, 0)
	check:SetNormalTexture("Interface\\Buttons\\UI-CheckBox-Up")
	check:SetPushedTexture("Interface\\Buttons\\UI-CheckBox-Down")
	check:SetHighlightTexture("Interface\\Buttons\\UI-CheckBox-Highlight")
	check:SetCheckedTexture("Interface\\Buttons\\UI-CheckBox-Check")
	check:RegisterForClicks("LeftButtonUp", "RightButtonUp")
	check:SetChecked(Atropine:getRefill(poison))
	check:SetScript("OnEnter", function()
		local c = count:GetNumber()
		local enable, q = Atropine:getRefill(poison)
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT")
		GameTooltip:SetText(L["AutoRefill"], (not enable and 1) or 0, (enable and 1) or 0, 0)
		GameTooltip:AddLine((L["AutoRefillValue"]):format(q or 0))
		GameTooltip:AddLine((L["AutoRefillTip"]):format(c))
		GameTooltip:Show()
	end)
	check:SetScript("OnLeave", function()
		GameTooltip:Hide()
	end)
	check:SetScript("OnClick", function(frame, button)
		local p, c = b.poison, count:GetNumber()
		if(IsShiftKeyDown()) then
			-- I has bucket (of ugly hacks...)
			this:SetChecked(not this:GetChecked())
			Atropine:updateRefillV(p, c)
		else
			Atropine:updateRefillS(p, this:GetChecked())
		end
	end)
	b.Check = check

	return b
end

Atropine.class.button = CreateFrame("Frame")
Atropine.class.button.core = Atropine

function Atropine.class.button:new(poison, texture, num)
--	self.core.numButtons = self.core.numButtons + 1
	local fname = "AtropineFrameButton"..num
	local frame = createFrame(fname, poison, texture, num)
	frame.poison = poison
	setmetatable(frame, {__index = Atropine.class.button})
	
	frame:setValues(poison)
	self:setPosition(frame, num)

	return frame
end

function Atropine.class.button:setPosition(frame, num)
	if(num == 1) then
		frame:SetPoint("TOPLEFT", self.core.frame, "TOPLEFT", 10, -64)
	else
		frame:SetPoint("TOP", self.core.frame[num-1], "BOTTOM", 0, 0)
	end

	self.core:updateFrame(num)
end

function Atropine.class.button:setValues(poison)
	self.Text:SetText(self.core:getFullName(poison))
	self.Count:SetText(20)
end

function Atropine.class.button:buy()
	local c = self.Count:GetNumber()

	self.core:add(self.poison, c)
	self.core:buy()
end

function Atropine.class.button:refill()
	local c = self.Count:GetNumber()

	self.core:add(self.poison, c, true)
	self.core:buy(refill)
end
